//
//  Extension+UIBarButtonItem.swift
//  SHWeibo3
//
//  Created by 余伟康 on 16/7/19.
//  Copyright © 2016年 wangce. All rights reserved.
//
//  分类 构造函数 自定义 UIBarButtonItem

import UIKit


extension UIBarButtonItem {

    // 便利构造函数
    convenience init(title:String? = nil,imgName :String? = nil,target :AnyObject?,action:Selector) {
        
        print(title)
        
        self.init()
        
        print(title)
        
        // 创建一个button
        let button = UIButton()
        
        // 判断是title 还是 image
        if let tit = title {
            button.setTitle(tit, forState: UIControlState.Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(15)
            button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        }
        
        if let img = imgName {
            button.setImage(UIImage(named: img), forState: UIControlState.Normal)

        }
        
        // 给butoon添加点击事件
        
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
        self.customView = button
    }
}




